package com.tiku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tiku.common.ResultJson;
import com.tiku.entity.*;
import com.tiku.mapper.ChapterInfoMapper;
import com.tiku.mapper.ChapterSubInfoMapper;
import com.tiku.mapper.QuestionTableMapper;
import com.tiku.mapper.UserCollectionMapper;
import com.tiku.service.MenuService;
import com.tiku.service.ProductSpecificationsService;
import com.tiku.service.UserErrorService;
import com.tiku.utils.RedisCache;
import com.tiku.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 小马不会跑
 * @create 2024-10-01 1:11
 */
@Service
public class UserErrorServiceImpl implements UserErrorService {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ChapterInfoMapper chapterInfoMapper;
    @Autowired
    private ChapterSubInfoMapper chapterSubInfoMapper;

    @Autowired
    private QuestionTableMapper tableMapper;

    @Autowired
    private UserCollectionMapper userCollectionMapper;

    @Autowired
    private MenuService menuService;

    @Autowired
    private ProductSpecificationsService productSpecificationsService;


    @Autowired
    private QuestionTableMapper questionTableMapper;

    @Override
    public ResultJson getUserError(String specification) {
        String userId = ThreadLocalUtil.getThreadLocal();
        Collection<String> keys = redisCache.keys("questionLog:" + userId + specification + "*");
        List<UserQuestionLog> logList = new ArrayList<>();
        for (String key : keys) {
           UserQuestionLog log= redisCache.getCacheObject(key);
           if (0==log.getRightflag()){
               logList.add(log);
               String questionId = log.getQuestionId();
               QuestionTable questionTable = tableMapper.selectById(questionId);
               Integer count = userCollectionMapper.selectCollectionByUserIdAndQuestionId(userId,questionId);
               if (count>0){
                   questionTable.setCollectFlagDO(true);
               }

               log.setQuestionTable(questionTable);

           }
        }


        logList.sort(Comparator.comparing(UserQuestionLog::getCreateTime).reversed());
        return ResultJson.success(logList);
    }

    @Override
    public ResultJson getErrorList(String specification,String menuId) {


        String userId = ThreadLocalUtil.getThreadLocal();
        Collection<String> keys = redisCache.keys("questionLog:" + userId + specification +menuId+  "*");
        List<UserQuestionLog> logList = new ArrayList<>();
        for (String key : keys) {
            UserQuestionLog log= redisCache.getCacheObject(key);
            if (0==log.getRightflag()){
                logList.add(log);
            }
        }

        Map<String, List<UserQuestionLog>> groupedLogs = logList.stream()
                .collect(Collectors.groupingBy(UserQuestionLog::getChapterInfoId));
        Set<String> strings = groupedLogs.keySet();
        List<Map<String,Object>> obj= new ArrayList<>();
        for (String key : strings) {
            Map<String, Object> newList=new HashMap<>();
            ChapterInfo chapterInfo = chapterInfoMapper.selectById(key);
            String chapterName=null;
            if(Objects.isNull(chapterInfo)){
                ChapterSubInfo chapterSubInfo = chapterSubInfoMapper.selectById(key);
                chapterName=chapterSubInfo.getChapterName();
            }else {
                chapterName=chapterInfo.getChapterName();
            }

            newList.put("chapterNameFather",chapterName);
            newList.put("list", groupedLogs.get(key));
            obj.add(newList);
        }

        return ResultJson.success(obj);
    }

    @Override
    public ResultJson errorRemove(String queueId,String chapterId) {

        String userId = ThreadLocalUtil.getThreadLocal();
        QuestionTable questionTable = questionTableMapper.selectById(queueId);
        String chapterIdText=questionTable.getChapterId();
        String chapterIdTwo=questionTable.getChapterId();
        while (true){
            ChapterSubInfo chapterSubInfo1 = chapterSubInfoMapper.selectById(chapterIdTwo);
            if (Objects.isNull(chapterSubInfo1)){
                break;
            }else {
                chapterIdTwo = chapterSubInfo1.getChapterId();

                chapterIdText=chapterIdTwo+chapterIdText;
            }

        }


        ChapterInfo chapterInfo = chapterInfoMapper.selectById(chapterIdTwo);


        String menuId = chapterInfo.getMenuId();
        Menu menu = menuService.getById(menuId);
        String psCategoryId = menu.getPsCategoryId();
        ProductSpecifications ps = productSpecificationsService.getOne(new LambdaQueryWrapper<ProductSpecifications>()
                .eq(ProductSpecifications::getCategoryId,psCategoryId));
        String specId = ps.getSpecid();

        //specid+menuId+chapterId2+chapterId+queueId
        redisCache.deleteObject("questionLog:"+userId+specId+menuId+chapterIdText+queueId);

        return ResultJson.success();
    }
}
